<!-- leecode 33 -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>


    <script>
        var arr = [0, 1, 2, 3, 4, 5, 6]

        function bSearch(arr, target) {
            let start = 0, end = arr.length - 1;
            while (start <= end) {
                const mid = Math.floor((end - start) / 2) + start;
                if (arr[mid] === target) {
                    return mid
                } else if (arr[mid] > target) {
                    end = mid - 1
                } else {
                    start = mid + 1
                }
            }
            return -1
        }

        console.log(bSearch(arr, 6))
    </script>
</body>

</html>